/**
* This file is part of tictionary.
*
* tictionary is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* http://code.google.com/p/tictionary/
*/

/*
 * DictUtils.java
 * Created on January 20, 2008, 1:41 PM
 */

package tictionary.dict;

/**
 *
 * @author Erol Hira
 */
public class OffsetComputer {
    
    private Dict dict;
    
    private int offset;        
    
    public OffsetComputer(Dict dict) {
        this.dict = dict;
    }
    
    public void calculateSelfOffsets(LetterSet letterSet){                                                
        
        if(letterSet == null){
            return;
        }
        
        offset ++; // 1 byte for letter count in the letterSet
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            letter.setSelfSetOffset(offset);
            
            if(letter.getChildSet() == null){
                offset += 3 + letter.getChars().length();
            } else {
                offset += Constants.LETTER_BYTE_COUNT;
            }            
        }
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            LetterSet childSet = letter.getChildSet();
            calculateSelfOffsets(childSet);
        }
    }
    
    public void calculateChildOffsets(LetterSet letterSet){
        
        if(letterSet == null){
            return;
        }
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            LetterSet childSet = letter.getChildSet();
            
            if(childSet == null || childSet.getLetterCount() == 0){
                letter.setChildSetOffset(-1);
            } else {
                
                //int letterOffset = letter.getSelfSetOffset();                
                
                //int childSetOffset = childSet.getLetter(0).getSelfSetOffset() - letterOffset;
                int childSetOffset = childSet.getLetter(0).getSelfSetOffset() - 1;
                
                letter.setChildSetOffset(childSetOffset);
            }
        }
        
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            LetterSet childSet = letter.getChildSet();
            calculateChildOffsets(childSet);
        }        
    }           
    
    public void calculateMeanOffsets(LetterSet letterSet){
        
        if(letterSet == null){
            return;
        }
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            
            if(letter.getMean() == null){               
                //System.out.println("null");
            } else {
                letter.setMeanOffset(offset);
                offset += 1 + Utils.string2bytes(letter.getMean()).length; // short(mean length) + letter bytes                
            }
        }
        
        for(int i = 0; i < letterSet.getLetterCount(); i++){
            
            Letter letter = letterSet.getLetter(i);
            
            LetterSet childSet = letter.getChildSet();
            calculateMeanOffsets(childSet);
        }
    }
        
    
    public void calculateOffsets(){
        
        LetterSet rootSet = dict.getRootSet();
        
        calculateSelfOffsets(rootSet);
        calculateChildOffsets(rootSet);  
        calculateMeanOffsets(rootSet);
        
        System.out.println("byteCount:" + offset);
    }
    
}
